<template>
  <el-dialog class="pick-dialog" title="转为问题号" :visible.sync="dialogVisible" :close-on-click-modal="false" width="700px"
    top="30vh">
    <div class="form-box" v-loading="loading">
      <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="auto">
        <el-form-item label="问题类型" prop="problemTypeName">
          <el-select v-model="ruleForm.problemTypeName">
            <el-option v-for="(item, i) in dict.type.problemType" :key="i" :label="item.label" :value="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题描述" prop="problemDes">
          <el-input type="textarea" :rows="4" v-model="ruleForm.problemDes" placeholder="请输入问题描述" style="width:500px" />
        </el-form-item>
        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import customerServiceApi from "@/api/pickUp/customerService.js";
import goodsListApi from "../../../api/gameConfig/goodsList";
import { postUpload } from "@/api/upload";
import ImageCompressor from 'image-compressor.js'
export default {
  name: "pick",
  dicts: [
    "problemType"
  ],
  data() {
    return {
      dialogVisible: false,
      info: "",
      ruleForm: {
        problemDes: null,//问题描述
        problemTypeName: null,//问题类型
        id: ''
      },
      loading: false,
      rules:{
        problemTypeName: [
          { required: true, message: "请选择问题类型", trigger: "change" }
        ],
      } 
    };
  },
  methods: {
    open(flag, data) {
      if (flag == 'zwt') {
        this.ruleForm.id = data.id
        // this.ruleForm.hszje = data
      } else if (flag == 'edit') {
        this.ruleForm = {...data}
      }
      this.dialogVisible = true;
    },
    close() {
      this.resetForm()
      this.dialogVisible = false;
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log("%c Line:97 🍌 ruleForm", "color:#e41a6a", this.ruleForm);
          this.loading = true
          goodsListApi.problemGoods(this.ruleForm).then(res => {
            this.dialogVisible = false
            this.loading = false
            if (res.code == 200) {
              console.log("%c Line:108 🍆 res", "color:#ffdd4d", res);
              this.$modal.msgSuccess(res.msg);
              this.$parent.getList()
              this.resetForm()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      })
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields();
    },
    // 图片预览
    handlePictureCardPreview(file) {
      this.$viewerApi({
        images: [file.url]
      });
    },
    /**以下是图片上传相关 */
    uploadNumber(file, fileList) {
      //限制图片数量
      this.$modal.msgError("最多只能上传10张图片");
    },
    // 压缩图片
    compressImage(file) {

      return new Promise((resolve, reject) => {
        new ImageCompressor(file.file, {
          quality: 0.6,
          checkOrientation: false,
          success(result) {
            resolve(result);
          },
          error(e) {
            reject();
          }
        });
      });
    },
    // 上传图片
    async uploadImgs(file) {
      // 获取压缩后的文件流
      let compreeBolb = await this.compressImage(file);
      const formData = new FormData();
      formData.append('file', compreeBolb);//压缩后的文件会自动转换成二进制文件流类型
      //上传图片到后台
      try {
        let uploadImg = await postUpload(formData)
        file.data = {
          filePath: uploadImg.msg
        }
        file.code = 200
        return (file)
      }
      catch {
        file.code = 500
        return (file)
      }
    },
    // 详情图片上传成功
    handleUploadSuccess(res, file, fileList) {
      if (res.code == 200) {
        // 取得新上传的文件
        this.newImages = fileList.filter(t => {
          return t.url.indexOf('blob:') > -1
        })
      }
    },
    // 详情图片删除
    handleRemove(response, fileList, file) {
      // console.log("%c Line:495 🍎 response", "color:#4fff4B", response);
      const that = this;
      // 1.获取将要删除图片的临时路径
      const filePath = response.url;
      // 2.找到图片对应的索引值
      this.imgFileList.forEach(function (element, key) {
        if (element.url === filePath) {
          that.imgFileList.splice(key, 1); //回显的图片
        }
      });
      this.newImages.forEach(function (element, key) {
        if (element.url === filePath) {
          that.newImages.splice(key, 1); //新上传的图片
        }
      });
    },



  }
};
</script>
<style scoped lang="css">
.center {
  text-align: center;
}

.pick-dialog>>>.el-dialog {
  margin-top: 100px !important;
}

.form-box {
  max-height: 800px;
  overflow-y: auto;
}

::v-deep .el-input-number.is-without-controls .el-input__inner {
  text-align: left;
}

.form-box>>>.el-upload-dragger {
  width: 148px;
  height: 148px;
}
</style>
